package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.service.IRoleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-08-14
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        roleMapper.add(role);
        initRelation(role.getId(),role.getMenus(),role.getPermissions());

    }

    private void initRelation(Long id, List<Long> menus, List<Long> permissions) {
        //删除role_menu表中的roleid的数据
        roleMapper.deleteRoleMenuByRoleId(id);
        //删除role_permission表中的roleid的数据
        roleMapper.deleteRolePermissionByRoleId(id);
        //保存role_menu
        roleMapper.addRoleMenu(id,menus);
        //保存role_permission
        roleMapper.addRolePermission(id,permissions);
    }
}
